package fun.flyee.android.pengshan.acts;

import android.app.AlertDialog;
import android.app.Notification;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;

import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;
import butterknife.OnClick;
import fun.flyee.android.pengshan.AppContex;
import fun.flyee.android.pengshan.Constant;
import fun.flyee.android.pengshan.Storage;
import fun.flyee.android.pengshan.api.ApiClient;

import fun.flyee.android.pengshan.events.AuthEvent;
import fun.flyee.android.pengshan.events.PhoneCallStateEvent;
import fun.flyee.android.pengshan.fgts.WebFragment;
import fun.flyee.android.pengshan.utils.NotificationUtils;
import fun.flyee.android.pengshan.utils.NotificationUtilsTimer;
import fun.flyee.android.pengshan.utils.Systems;
import fun.flyee.android.pengshan.widgets.Tips;
import fun.flyee.pengshan.social.BuildConfig;
import fun.flyee.pengshan.social.R;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class LoginActivity extends BaseActivity {

    private WebFragment webFragment;

    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        if (b == null) {
            webFragment = WebFragment.newInstance("/login", "");
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.frame_container, webFragment, "fragment_web").show(webFragment);
            transaction.commitAllowingStateLoss();
        } else {
            webFragment = (WebFragment) getSupportFragmentManager().findFragmentByTag("fragment_web");
            webFragment.setPath("/login");
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.show(webFragment);
            transaction.commitAllowingStateLoss();
        }
    }

    @Override
    protected void onPostCreate(Bundle bundle) {
        super.onPostCreate(bundle);
        Systems.requestPermission(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Systems.requestPermission(this);
    }

    @Subscribe
    public void onPhoneCall(PhoneCallStateEvent ev) {
        if (BuildConfig.DEBUG) {
            new AlertDialog.Builder(this).setMessage(ev.state).setCancelable(true).setPositiveButton("Close", (d, i) -> {
                d.dismiss();
            }).show();
        }
    }

    @Override
    protected int layoutId() {
        return R.layout.act_container;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
    }

    @Subscribe
    public void onAuth(AuthEvent ev) {
        Router.main(this);
        finish();
    }
}
